package com.sleep.psy.external_server.business.resourse.service.impl;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sleep.psy.external_server.business.resourse.entity.QuestAnswer;
import com.sleep.psy.external_server.business.resourse.entity.QuestMain;
import com.sleep.psy.external_server.business.resourse.mapper.QuestMainMapper;
import com.sleep.psy.external_server.business.resourse.service.QuestAnswerService;
import com.sleep.psy.external_server.business.resourse.service.QuestMainService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 试题表 服务实现类
 * </p>
 *
 * @author wenliguo
 * @since 2025-08-12
 */
@Service
public class QuestMainServiceImpl extends ServiceImpl<QuestMainMapper, QuestMain> implements QuestMainService {

    protected Logger logger = LoggerFactory.getLogger(QuestMainService.class);

    @Autowired
    private QuestMainMapper questMainMapper;

    @Autowired
    private QuestAnswerService questAnswerService;

    /**
     * 通过症状获取题目和答案
     * @author wenliguo
     * @date 2025/6/5 10:56
     * @param types 症状id(多个值用,隔开)
     * @return com.sleep.psy.main_server.result.Result
     */
    @Override
    public List<QuestMain> selectByType(String types) {

        List<String> list = Arrays.asList(types.split(","));
        List<QuestMain> questMains = questMainMapper.selectMainByType(list);

        if (questMains.isEmpty()) {
            return null;
        }

        logger.info("通过症状获取的所有问题:{}",questMains.size());

        Map<String, List<QuestAnswer>> anserMap = questAnswerService.getAnserMap();
        if (null == anserMap) {
            return null;
        }

        for (QuestMain questMain : questMains) {
            questMain.setAnswerList(anserMap.get(questMain.getId() + ""));
        }

        return questMains;

    }
}
